<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
	 <meta charset="utf-8">
	 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	 <title th:replace="common/base::pagetitle"></title>
	 <link th:replace="common/base::static"/>
</head>
<body style="background-color: #f2f2f2;">

<div id="kPage" class="layui-fluid" style="padding: 0">
	 <div class="layui-row">
		  <div class="layui-col-md12" id="kMainContentCol">
				<div class="layui-fluid wlb-fluid-padding15">
					 <div class="layui-card">
						  <form>
								<div class="layui-form layui-card-header wlb-card-header-auto">
									 <div class="layui-form-item">
										  <div class="layui-inline">
												<label class="layui-form-label">申请码</label>
												<div class="layui-input-inline">
													 <input type="text" name="code" placeholder="请输入申请码" autocomplete="off" class="layui-input">
												</div>
										  </div>
										  
										  <div class="layui-inline">
												<button class="layui-btn " lay-submit="" lay-filter="kTableSearchFilter">
													 <i class="layui-icon layui-icon-search"></i>
												</button>
												<button class="layui-btn " type="reset">
													 <i class="layui-icon layui-icon-refresh-1"></i>
												</button>
										  </div>
									 </div>
								</div>
						  </form>
						  
						  <div class="layui-card-body">
								<table id="kContentTable" lay-filter="kContentTable"></table>
						  </div>
					 </div>
				
				</div>
		  </div>
	 </div>
</div>

<script th:replace="common/base::context"></script>
<script th:replace="common/base::mainjs"></script>

<!--头部工具栏模板-->
<script type="text/html" id="headToolbarTpl">
	 <button class="layui-btn layui-btn-sm " lay-event="add"><i class=""></i>创建</button>
	 <!--<button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="batchdel"><i class=""></i>批量删除</button>-->
</script>

<!--表格行工具栏模板-->
<script type="text/html" id="rowToolbarTpl">
	 <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit"><i class="layui-icon layui-icon-download-circle"></i>授权文件</a>
	 <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del"><i class="layui-icon layui-icon-download-circle"></i>源文件</a>
</script>

<!--表格列格式化模板 start-->
<!--<script type="text/html" id="timeTpl">-->
<!--&lt;!&ndash;todo由于注释不统一规范，无法为你生成格式化代码，请自行处理&ndash;&gt;-->
<!--<span>{{d.time}}</span>-->
<!--<button class="layui-btn layui-btn-sm" lay-event="down"><i class=""></i>下载</button>-->
<!--</script>-->
<script type="text/html" id="unitTpl">
	 {{#  if(d.unit == "h"){ }}
	 <span class="layui-badge layui-bg-blue">小时</span>
	 {{# } else if(d.unit == "d") { }}
	 <span class="layui-badge layui-bg-blue">天</span>
	 {{# } else if(d.unit == "y") { }}
	 <span class="layui-badge layui-bg-blue">年</span>
	 {{# } }}
</script>

<!--表格列格式化模板 end-->

<script type="text/javascript" th:inline="javascript">
    layui.use(['element', 'form', 'layer', 'table', 'laydate'], function () {
        var elemet = layui.element, form = layui.form, layer = layui.layer, table = layui.table, laydate = layui.laydate;
        var tableId = 'kContentTable';
        var area = ['449px', '90%'];

        // 实例化table
        var tableIns = table.render($.extend(config.layui.table, {
            elem: '#' + tableId
            , url: BASE_WEB + 'license/server/list/data' // 数据接口
            , toolbar: '#headToolbarTpl'
            , cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                , {field: 'code', title: '申请码', width: 180}
                , {field: 'time', title: '授权有效期', width: 100}
                , {field: 'unit', title: '单位', width: 100, templet: '#unitTpl'}
                , {field: 'applyTime', title: '申请时间', width: 180}
                , {field: 'expireTime', title: '过期时间', width: 180}
                , {field: 'createTime', title: '创建时间', width: 180}
                , {field: 'remark', title: '备注', width: 180}
                , {field: '', title: '操作', width: 235, fixed: 'right', templet: '#rowToolbarTpl'}
            ]]
        }));

        // 监听表格头部事件
        table.on('toolbar(' + tableId + ')', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var checkData = checkStatus.data; // 得到选中的数据
            switch (obj.event) {
                case 'add':
                    layer.open({
                        type: 2
                        , title: '创建'
                        , content: BASE_WEB + 'license/server/edit'
                        , maxmin: true
                        , area: area   // 宽高
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            //点击确认触发 iframe 内容中的按钮提交
                            var submit = layero.find('iframe').contents().find("#kFormSubmit");
                            submit.click();
                        }
                    });
                    break;
                case 'batchdel':
                    if (checkData.length === 0) {
                        return wlbKit.warningMsg('请选择数据');
                    }
                    layer.confirm('确定删除吗？', function (index) {
                        var ids = [];
                        $.each(checkData, function (i, n) {
                            ids[i] = n.id;
                        });

                        //执行 Ajax 后重载
                        $.ajax({
                            type: req.type.post,
                            url: BASE_WEB + 'isv/license/batchdel',
                            data: {ids: ids.toString()},
                            success: function (r) {
                                if (r.code === req.status.ok) {
                                    table.reload(tableId);
                                    wlbKit.sucessMsg('已删除');
                                } else {
                                    wlbKit.errorMsg(r.msg);
                                }
                            }
                        });
                    });
                    break;
            }
        });

        // 表格行按钮 监听事件
        table.on('tool(' + tableId + ')', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent === 'edit') {
                // 下载license文件
                window.open(BASE_WEB + 'license/server/downLicense/' + data.id);
                // layer.open({
                //     type: 2
                //     , title: '下载源文件'
                //     , content: BASE_WEB + 'isv/license/edit?id=' + data.id
                //     , maxmin: true
                //     , area: area   // 宽高
                //     , btn: ['确定', '取消']
                //     , yes: function (index, layero) {
                //         //点击确认触发 iframe 内容中的按钮提交
                //         var submit = layero.find('iframe').contents().find("#kFormSubmit");
                //         submit.click();
                //     }
                // });
            }
            if (layEvent === 'del') {
                // 下载数据
                window.open(BASE_WEB + 'license/server/downSource/' + data.id);
                // layer.confirm('确定删除吗？', function (index) {
                //     //执行 Ajax 后重载
                //     $.ajax({
                //         type: req.type.post,
                //         url: BASE_WEB + 'isv/license/del/' + data.id,
                //         async: false,
                //         success: function (r) {
                //             if (r.code === req.status.ok) {
                //                 table.reload(tableId);
                //                 wlbKit.sucessMsg('已删除');
                //             } else {
                //                 wlbKit.errorMsg(r.msg);
                //             }
                //         }
                //     });
                // });
            }
        });


        // 监听查询
        form.on('submit(kTableSearchFilter)', function (obj) {
            var params = obj.field;
            tableIns.reload({
                where: params,
                page: {curr: 1}
            });
            return false;
        });

        // 绑定监听回车事件
        wlbKit.bindEnterEventForLayuiForm();

    });
</script>

</body>
</html>